#!/usr/bin/python
#This python script will print the pieces of a bencoded file. Pipe this output to a file to save and read it.
import sys
import string
import re
decimal_match = re.compile('\d')

def bdecode(data):
    '''Main function to decode bencoded data'''
    chunks = list(data)
    chunks.reverse()
    root = _dechunk(chunks)
    return root

def _dechunk(chunks):
    item = chunks.pop()

    if item == 'd': 
        item = chunks.pop()
        hash = {}
        while item != 'e':
            chunks.append(item)
            key = _dechunk(chunks)
            hash[key] = _dechunk(chunks)
            item = chunks.pop()
        return hash
    elif item == 'l':
        item = chunks.pop()
        list = []
        while item != 'e':
            chunks.append(item)
            list.append(_dechunk(chunks))
            item = chunks.pop()
        return list
    elif item == 'i':
        item = chunks.pop()
        num = ''
        while item != 'e':
            num  += item
            item = chunks.pop()
        return int(num)
    elif decimal_match.search(item):
        num = ''
        while decimal_match.search(item):
            num += item
            item = chunks.pop()
        line = ''
        for i in range(int(num)):
            line += chunks.pop()
        return line
    raise "Invalid input!"print ""


f = open(sys.argv[1],"rb")
data = f.read()
d = bdecode(data)
print d,"\n\n"

for key in  d.keys():
	print key+" -",
	if type(d[key]) is dict:
		for i in d[key].keys():
			print "\t"+i+" -", d[key][i]
	else:
		print  d[key]

